///@file Truss2.cpp
///@brief Implementation of the class representing a truss element with 2 nodes
///@author Arnaud Duval
///@version 0.0
///@date 2010/02/26

#include "Truss2.h"

#include <cmath>


///@brief Constructor specifying the nodes
Truss2::Truss2(Node* n1, Node* n2)
{
	nodes.push_back(n1);
	nodes.push_back(n2);
	nbGauss = 2u;
}

///@brief Constructor with arguments
///@param n1 Pointer to first node
///@param n2 Pointer to second node
///@param material affected material
///@param sec affected section
Truss2::Truss2(Node* n1, Node* n2, Material* material, double sec)
{
	assert(n1->Dim() == n2->Dim());
	nodes.push_back(n1);
	nodes.push_back(n2);
	mat = material;
	section = sec;	
}

///@brief Destructor
Truss2::~Truss2()
{
    std::cout << "Destructeur de Truss2\n";
	for(std::list<Node*>::iterator i = nodes.begin(); i != nodes.end() ; i++)
	{
		*i = NULL;
	}
	mat = NULL;
    std::cout << "Fin du destructeur de Truss2\n";
}

///@brief Update specific values of element
void Truss2::Update()
{
	///@brief Check the dimension of the problem
	std::list<Node*>::iterator n = nodes.begin();
	dimension = (*n)->Dim();
	///@brief Compute length of element
	length = Distance(**n, **(n++));
}


///@brief compute nodal residual from nodal DOF increment
///@param Du Increment of nodal DOF
///@param Df nodal residuals
///@param K stiffness matrix
void Truss2::Equilibrate(const Vector& Du, Vector& Df, Matrix& K)
{
	std::cout << "Appel de la methodes Equilibrate pour Truss2\n";

}

///@brief Compute displacements interpolation matrix at a given intagration point
///@param idx index of integration point where the matrix should be computed
Matrix Truss2::N(unsigned int idx)
{
	assert((idx ==1)||(idx==2));
	Matrix res((unsigned int)2,(unsigned int)1);
	double xi;
	if(idx == 1)
		xi =-1./sqrt(3.);
	else if(idx == 2)
		xi = 1./sqrt(3.);

	res(1,1) = (1. - xi)/2.;
	res(2,1) = (1. + xi)/2.;

	return res;
}

///@brief Compute strain matrix operator at a given integration point
///@param idx index of integration point where the matrix should be computed
Matrix Truss2::B(unsigned int idx)
{
	assert((idx ==1)||(idx==2));
	Matrix res((unsigned int)2,(unsigned int)1);
	double xi;
	if(idx == 1)
		xi =-1./sqrt(3.);
	else if(idx == 2)
		xi = 1./sqrt(3.);

	res(1,1) = -1./length;
	res(2,1) = 1./length;

	return res;
}




